package com.hoshiicloud.vericenter.controller;

import com.hoshiicloud.common.validate.annotations.IdCard;
import com.hoshiicloud.vericenter.result.Response;
import com.hoshiicloud.vericenter.util.ALiYunUtil;
import com.hoshiicloud.vericenter.util.RegexUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * 用户验证控制器
 *
 * @author Mr.wu
 *
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户验证接口")
@Slf4j
@Validated
public class UserVeriController {

	@RequestMapping(value = "/veriReal", method = {RequestMethod.GET })
	@ApiOperation(value = "用户实名验证接口")
	@ApiImplicitParams({ @ApiImplicitParam(name = "realName", value = "真实姓名", dataType = "String", required = true,example = "张三"),
			@ApiImplicitParam(name = "idNo", value = "身份证号码", dataType = "String", required = true,example = "1234") })
	public Response userVeri(@RequestParam @NotBlank String realName,@RequestParam @IdCard String idNo) {
		/**
		 * 如验证出错方法已抛出异常，此处直接返回
		 */
		Map<String,Object> resultMap= ALiYunUtil.getPersonVerificationMessage(realName,idNo);
		return Response.success("认证通过");
	}

	@RequestMapping(value = "/veriBank", method = {RequestMethod.GET })
	@ApiOperation(value = "银行卡三要素验证接口")
	@ApiImplicitParams({ @ApiImplicitParam(name = "realName", value = "真实姓名", dataType = "String", required = true,example = "张三"),
			@ApiImplicitParam(name = "idNo", value = "身份证号码", dataType = "String", required = true,example = "123"),
			@ApiImplicitParam(name = "cardNo", value = "银行卡号", dataType = "String", required = true,example = "123") })
	public Response bankInfoThreeElementVeri(@RequestParam @NotBlank String realName,@RequestParam @IdCard String idNo,
			@RequestParam @NotBlank String cardNo) {
		/**
		 * 如验证出错方法已抛出异常，此处直接返回
		 */
		Map<String,Object> resultMap=ALiYunUtil.getBankCardThreeElementVerificationMessage(realName,idNo,cardNo);
		//如果需要银行卡信息再做返回
		return Response.success("认证通过");
	}


	@RequestMapping(value = "/idCardOcr",method = RequestMethod.GET)
	@ApiOperation(value = "身份证ocr识别接口",notes = "data正常返回内容见接口文档:https://market.aliyun.com/products/57124001/cmapi010401.html")
	@ApiImplicitParams({ @ApiImplicitParam(name = "image", value = "图片url或图片base64字符串", dataType = "String", required = true,example = ""),
			@ApiImplicitParam(name = "side", value = "身份证正反面 face或back", dataType = "String", required = true,example = "face") })
	public Response idCardOcrDiscern(@RequestParam @NotBlank String image,@RequestParam @NotBlank String side){
		//判断image是否为url
		//判断image是否为base64字符串
		if(!RegexUtil.isUrl(image) && !RegexUtil.isBase64Img(image)){
			return Response.failed("image需为图片url或图片base64字符串");
		}
		//判断身份证正反面类型:face/back
		if("face".equals(side) || "back".equals(side)){
			return Response.data(ALiYunUtil.getIdCardVerificationMessage(image,side));
		}
		else{
			return Response.failed("错误的类型");
		}

	}



}
